Page({
  data: {
    user: '',
    loading: true,
    sexShow: false,
    sexList: [{
        text: '男',
        value: '男'
      },
      {
        text: '女',
        value: '女'
      }
    ],
    info: {
      user: '',
      pass: '',
      city: '',
      avatar: 'cloud://tourplan-2gaddhh5ba2cfaec.746f-tourplan-2gaddhh5ba2cfaec-1322500727/景点图片/user.png',
    }
  },
  onChooseAvatar: async function(event) {
    const { avatarUrl } = event.detail;
    try {
      // 上传选择的头像到云存储
      const uploadResult = await wx.cloud.uploadFile({
        cloudPath: 'avatar/' + new Date().getTime() + '.png',
        filePath: avatarUrl
      });
      
      const fileID = uploadResult.fileID;
      
      // 更新页面中头像链接的数据
      this.setData({
        'info.avatar': fileID
      });

      // 立即更新数据库中的头像
      const db = wx.cloud.database();
      await db.collection('zyzl_userList').where({
        user: this.data.user
      }).update({
        data: {
          avatar: fileID
        }
      });
      
      // 提示上传成功
      wx.showToast({
        title: '头像更新成功',
        icon: 'success'
      });

      // 更新本地存储的头像
      let userInfo = wx.getStorageSync('userInfo') || {};
      userInfo.avatar = fileID;
      wx.setStorageSync('userInfo', userInfo);

    } catch (error) {
      console.error('头像更新失败', error);
      wx.showToast({
        title: '头像更新失败，请稍后重试',
        icon: 'none'
      });
    }
  },
  
  onLoad: async function(options) {
    try {
      const user = wx.getStorageSync('user') || ''; // 获取本地缓存的用户名
      this.setData({ user });
      // console.log(this.data.user)
      const db = wx.cloud.database();
      const userInfo = await db.collection('zyzl_userList').where({
        user: this.data.user
      }).get();
      if (userInfo.data.length > 0) {
        // 找到了匹配的用户数据
        console.log("123",userInfo)
        const userData = userInfo.data[0];
        this.setData({
          'info.user': userData.user,
          'info.pass': userData.pass,
          'info.avatar': userData.avatar,
          'info.phone': userData.phone,    
          'info.city': userData.city,      
          loading: false
        });
      } else {
        // 没有找到匹配的用户数据
        wx.showToast({
          title: '用户数据不存在',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('获取用户数据失败', error);
      wx.showToast({
        title: '获取用户数据失败，请稍后重试',
        icon: 'none'
      });
    }
    console.log(this.data.user)
  },

  formSubmit(e) {
    var info = e.detail.value;
    if (info.user !== '' && info.pass !== '' && info.phone !== '' && info.city !== '') {
      if (info.avatar !== '') {
        wx.showModal({
          title: '确认修改',
          content: '您确定要修改用户信息吗？',
          success: (res) => {
            if (res.confirm) {
              // 用户点击了确定按钮
              // 将头像信息添加到用户信息中
              info.avatar = this.data.info.avatar;
              this.updateUserInfo(info);
            }
          }
        });
      } else {
        wx.showToast({
          title: '头像不能为空',
          icon: 'none'
        });
      }
    } else {
      wx.showToast({
        title: '必填项不能为空',
        icon: 'none'
      });
    }
  },

  async updateUserInfo(userInfo) {
    try {
      const db = wx.cloud.database();
      await db.collection('zyzl_userList').where({
        user: this.data.user
      }).update({
        data: {
          user: userInfo.user,
          pass: userInfo.pass,
          avatar: userInfo.avatar,
          phone: userInfo.phone,    
          city: userInfo.city       
        }
      });
  
      wx.showToast({
        title: '更新成功',
        icon: 'success',
        success: () => {
          // 返回上一个页面
          wx.navigateBack({
            delta: 1
          });
        }
      });
    } catch (error) {
      console.error('更新用户信息失败', error);
      wx.showToast({
        title: '更新用户信息失败，请稍后重试',
        icon: 'none'
      });
    }
  },

});
